import { AuthUnionType, RESET_SINGNUP, SIGNIN, SIGNIN_FAIL, SIGNIN_SUCCESS, SIGNUP, SIGNUP_FAIL, SIGNUP_SUCCESS } from "../actions/auth.actions";

export interface AuthState {
    signup:{
        loaded: boolean,
        success: boolean,
        message?:string
    },
    signin:{
        loaded: boolean,
        success: boolean,
        message?:string
    }
}
const initialState : AuthState = {
    signup:{
        loaded: false,
        success: false,
        message:''
    },
    signin:{
        loaded: false,
        success: false,
        message:''
    }
}
export default function authReducer(state: AuthState = initialState, action: AuthUnionType) {
    switch (action.type) {
        case SIGNUP:
            return {
                ...state,
                signup:{
                    loaded: false,
                    success: false
                }
            }
        case SIGNUP_SUCCESS:
            return {
                ...state,
                signup:{
                    loaded: true,
                    success: true
                }
            }
        case SIGNUP_FAIL:
            return {
                ...state,
                signup:{
                    loaded: true,
                    success: false,
                    message: action.message
                }
            }
        case RESET_SINGNUP:
            return {
                ...state,
                signup: initialState.signup
            }
        case SIGNIN:
            return {
                ...state,
                signin: {
                    loaded:false,
                    success:false
                }
            }
        case SIGNIN_SUCCESS:
            return {
                ...state,
                signin: {
                    loaded:true,
                    success:true
                }
            }
        case SIGNIN_FAIL:
        return {
            ...state,
            signin: {
                loaded:true,
                success:false,
                message: action.message
            }
        }
        default:
            return state;
    }
}